package me.zhengjie.modules.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import me.zhengjie.modules.utils.chat.BaiDuMessage;
import me.zhengjie.modules.utils.chat.BaiDuToken;
import me.zhengjie.utils.http.HttpUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BaiDuChatUtils {

    private BaiDuChatUtils() {
    }

    private static class BaiDuChatUtilsHolder {
        private static final BaiDuChatUtils INSTANCE = new BaiDuChatUtils();
    }

    public static final BaiDuChatUtils getInstance() {
        return BaiDuChatUtils.BaiDuChatUtilsHolder.INSTANCE;
    }

    public String getToken(){
        String result = HttpUtils.get(ApiConstant.getToken("2GmsANiTqFC6ZrP4dJX729UF", "SKLvMUZyj8usUKznW4owsouZxL5I5bnG"));
        BaiDuToken baiDuToken = JSON.parseObject(result, BaiDuToken.class);
        return baiDuToken.getAccessToken();
    }


    public String chat(String question){
        Map<String,String> headMap = new HashMap<>();
        headMap.put("Content-Type","application/json");
        Map<String,Object> dataMap = new HashMap<>();
        List<BaiDuMessage> list = new ArrayList<>();
        BaiDuMessage message = new BaiDuMessage();
        message.setContent(question);
        list.add(message);
        dataMap.put("messages",list);
        //dataMap.put("stream",true);
        //String url = ApiConstant.ERNIE_BOT_4 + "24.03b5b197a54e9eb1c3c9fca51e2c9bb4.2592000.1715926462.282335-60027096";
        String url = ApiConstant.ERNIE_BOT_TURBO + "24.03b5b197a54e9eb1c3c9fca51e2c9bb4.2592000.1715926462.282335-60027096";
        HttpResponse execute = HttpUtil.createPost(url).addHeaders(headMap).body(JSON.toJSONString(dataMap)).execute();
        System.out.println(execute.body());
        return "";
    }
    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        //System.out.println(getInstance().getToken());
        getInstance().chat("atmr 心智");
        System.out.println(System.currentTimeMillis()-l);
    }
}
